/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package general;

import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author foliveira
 */
public final class UDPConnection implements Connection {

  private DatagramSocket socket;
  private int port;

  public DatagramSocket getSocket() {
    return socket;
  }

  public void setSocket(DatagramSocket socket) {
    this.socket = socket;
  }

  public int getPort() {
    return port;
  }

  public void setPort(int port) {
    this.port = port;
  }
  
  public UDPConnection() {
    open();
  }

  public UDPConnection(int port) {
    this.port = port;
    open(port);
  }

  @Override
  public void open() {
    try {
      socket = new DatagramSocket();
    } catch (SocketException ex) {
      ex.printStackTrace();
    }
  }

  @Override
  public void open(int port) {
    try {
      socket = new DatagramSocket(port);
    } catch (SocketException ex) {
      ex.printStackTrace();
    }
  }

  @Override
  public void close() {
    if (socket != null) {
      socket.close();
    }
  }

  @Override
  public void setTimeOut(int time) throws Exception {
    socket.setSoTimeout(time);
  }
}
